package tech.aichan.AiQA.controller;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import tech.aichan.AiQA.common.BaseResponse;
import tech.aichan.AiQA.common.DeleteRequest;
import tech.aichan.AiQA.model.dto.app.AppAddRequest;
import tech.aichan.AiQA.model.dto.app.AppEditRequest;
import tech.aichan.AiQA.model.dto.app.AppQueryRequestES;
import tech.aichan.AiQA.model.dto.app.AppUpdateRequest;
import tech.aichan.AiQA.model.entity.App;
import tech.aichan.AiQA.model.vo.UserVO;
import tech.aichan.AiQA.model.vo.app.AppVO;
import tech.aichan.AiQA.service.AppService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import tech.aichan.AiQA.service.UserService;


/**
* 应用接口
*
* @author mengyu
* @date 2024/06/03 10:13
*/
@RestController
@RequestMapping("/app")
@Slf4j
public class AppController {

    @Resource
    private AppService appService;
    @Resource
    private UserService userService;

    // region 增删改查

    /**
     * 创建应用
     *
     * @param appAddRequest
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addApp(@RequestBody @Valid AppAddRequest appAddRequest) {
        Long l = appService.addApp(appAddRequest);
        return BaseResponse.success(200,l);
    }

    /**
     * 删除应用
     *
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteApp(@RequestBody DeleteRequest deleteRequest) {
        appService.deleteApp(deleteRequest.getId());
        return BaseResponse.success(200,true);
    }

    /**
     * 更新应用（仅管理员可用）
     *
     * @param appUpdateRequest
     * @return
     */
    @PostMapping("/update")
    @PreAuthorize("hasAnyRole('admin')")
    public BaseResponse<Boolean> updateApp(@RequestBody AppUpdateRequest appUpdateRequest) {
        return BaseResponse.success(200,true);
    }

    /**
     * 根据 id 获取应用（封装类）
     *
     * @param id
     * @return
     */
    @GetMapping("/get/vo/{id}")
    public BaseResponse<AppVO> getAppVOById(@PathVariable Long id) {
        App app = appService.getById(id);
        return BaseResponse.success(200,AppVO.getAppVOByApp(app, userService.getById(app.getUserId())));
    }

    /**
     * 根据 id 获取应用
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAnyRole('admin')")
    @GetMapping("/get/{id}")
    public BaseResponse<App> getAppById(@PathVariable Long id) {
        App app = appService.getById(id);
        return BaseResponse.success(200,app);
    }

    /**
     * 分页获取应用列表（仅管理员可用）
     *
     * @param appQueryRequest
     * @return
     */
    @PostMapping("/list/page")
    @PreAuthorize("hasAnyRole('admin')")
    public BaseResponse<Page<App>> listAppByPage(@RequestBody @Valid AppQueryRequestES appQueryRequest) {
        String appName = appQueryRequest.getAppName();
        Long userId = appQueryRequest.getUserId();
        Integer appType = appQueryRequest.getAppType();
        Boolean dateAsc = appQueryRequest.getDateAsc();
        int current = appQueryRequest.getCurrent();
        int pageSize = appQueryRequest.getPageSize();
        Page<App> appPage = appService.listPageApp(appName, userId, appType, current, pageSize, dateAsc);
        return BaseResponse.success(200,appPage);
    }

    /**
     * 分页获取应用列表（封装类）
     *
     * @param appQueryRequest
     * @return
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<AppVO>> listAppVOByPage(@RequestBody @Valid AppQueryRequestES appQueryRequest) {
        String appName = appQueryRequest.getAppName();
        Integer appType = appQueryRequest.getAppType();
        Boolean dateAsc = appQueryRequest.getDateAsc();
        int current = appQueryRequest.getCurrent();
        int pageSize = appQueryRequest.getPageSize();
        Page<AppVO> appVOPage = appService.listPageAppVO(appName, null, appType, current, pageSize, dateAsc);
        return BaseResponse.success(200,appVOPage);
    }

    /**
     * 分页获取当前登录用户创建的应用列表
     *
     * @param appQueryRequest
     * @return
     */
    @PostMapping("/my/list/page/vo")
    public BaseResponse<Page<AppVO>> listMyAppVOByPage(@RequestBody @Valid AppQueryRequestES appQueryRequest) {
        String appName = appQueryRequest.getAppName();
        Long userId = appQueryRequest.getUserId();
        Integer appType = appQueryRequest.getAppType();
        Boolean dateAsc = appQueryRequest.getDateAsc();
        int current = appQueryRequest.getCurrent();
        int pageSize = appQueryRequest.getPageSize();
        Page<AppVO> appVOPage = appService.listPageAppVO(appName, userId, appType, current, pageSize, dateAsc);
        return BaseResponse.success(200,appVOPage);
    }

    /**
     * 编辑应用（给用户使用）
     *
     * @param appEditRequest
     * @return
     */
    @PostMapping("/edit")
    public BaseResponse<Boolean> editApp(@RequestBody AppEditRequest appEditRequest) {
        return BaseResponse.success(200,true);
    }

    // endregion
}
